<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/29 11:45
 * Remark:
 */

namespace App\ServiceChecker\Department;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Company\CompanyModel;
use App\Models\Department\DepartmentModel;
use App\ServiceChecker\BasicServiceChecker;
use Illuminate\Http\Exceptions\HttpResponseException;

class DeptLegitimacy extends BasicServiceChecker
{
    private DepartmentModel $_departmentModel;

    public function __construct()
    {
        parent::__construct();
        $this->_departmentModel = new DepartmentModel();
    }

    /**
     * 校验部门名称是否存在
     * @param $departmentName
     * @param $companyId
     */
    public function verifyCompanyDepartmentNameIsExists($departmentName, $companyId){
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_name' => $departmentName,
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '部门名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 修改时检验部门名称是否存在
     * @param $departmentId
     * @param $departmentName
     */
    public function verifyDepartmentNameIsExistsByEdit($departmentId, $departmentName){
        $departmentInfo = $this->_departmentModel->getDepartmentInfoByDepartmentIdModel($departmentId);
        $resDepartmentInfo = $this->_departmentModel->getDepartmentInfoModelByDepartmentName($departmentName,["department_id","company_id"]);
        if($resDepartmentInfo && $resDepartmentInfo->department_id != $departmentId && $departmentInfo->company_id == $resDepartmentInfo->company_id){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '部门名已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 校验公司下部门是否存在
     * @param $departmentId
     */
    public function verifyCompanyDepartmentIsExists($departmentId, $companyId){
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_id' => $departmentId,
            'company_id' =>$companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "父部门不存在或已删除",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,

            ));
        }
    }

    /**
     * 校验部门是否存在
     * @param $departmentId
     */
    public function verifyDepartmentIsExists($departmentId){
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_id' => $departmentId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前部门不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,

            ));
        }
    }

    /**
     * 校验当前部门是否为它部门父级
     * @param $departmentId
     * @param $msg
     */
    public function verifyDepartmentParentIsExists($departmentId,$msg){
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_parent_id' => $departmentId,
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                $msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,

            ));
        }
    }

    /**
     * 修改时检验部门名称是否存在
     * @param $companyId
     * @param $companyName
     */
    public function verifyDeptNameIsExistsByEdit($departmentId, $departmentName){
        $editIds = $this->_departmentModel->setGetEditCheckerIds('department_id',$departmentId);
        // 获取当前部门公司
        $companyId = $this->_departmentModel->getDepartmentInfoByDepartmentIdModel($departmentId,['company_id']) ?? 0;
        $editNames = $this->_departmentModel->setGetEditCheckerNames('department_name',$departmentName);
        $isExists = $this->_departmentModel->editNameChecker($editNames,$editIds,[
            $this->_departmentModel->setGetEditCheckerCondition('company_id',$companyId)
        ]);
        if($isExists){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前部门名称已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 校验当前部门是否为它部门父级
     * @param $departmentId
     * @param $msg
     */
    public function verifyDepartmentHasChildIsExists($departmentId){
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_parent_id' => $departmentId,
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '当前部门存在子部门不能进行此操作',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }


    public function verifyDepartmentParentIsLegal($deptId,$parentId){
        // 校验父公司不能为自己
        if($deptId == $parentId){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '父公司不能为自己',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        // 校验当前部门是否存在子节点
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_parent_id' => $deptId,
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '当前部门存在子节点不能修改',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        // 校验当前父部门是否存在
        $isExist = $this->_departmentModel->isExistByCondition([
            'department_id' => $parentId,
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                '父部门不存在或已删除',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }
}